const Redis = require('ioredis');

// Redis配置
const redisConfig = {
  port: 6379,
  host: '127.0.0.1',
  password: '',
  db: 0,
};

const redis = new Redis(redisConfig);

async function checkSpecificBatch() {
  try {
    console.log('🔍 连接到Redis...');
    
    // 检查连接
    await redis.ping();
    console.log('✅ Redis连接成功');
    
    // 我们的批次ID
    const targetBatchId = 'batch_1763968563435_4r6i70dcf';
    
    // 检查批次数据键
    const batchDataKey = `plugin_data_batch:${targetBatchId}`;
    const batchInfoKey = `plugin_data_batch_info:${targetBatchId}`;
    
    console.log(`\n🔍 查找批次: ${targetBatchId}`);
    
    // 检查批次数据
    const batchDataExists = await redis.exists(batchDataKey);
    console.log(`\n📦 批次数据键 (${batchDataKey}): ${batchDataExists ? '存在' : '不存在'}`);
    
    if (batchDataExists) {
      const batchData = await redis.get(batchDataKey);
      console.log('📦 批次数据内容:');
      try {
        const parsedData = JSON.parse(batchData);
        console.log(JSON.stringify(parsedData, null, 2));
      } catch (e) {
        console.log('数据不是有效的JSON格式:', batchData.substring(0, 200) + '...');
      }
    }
    
    // 检查批次信息
    const batchInfoExists = await redis.exists(batchInfoKey);
    console.log(`\n📋 批次信息键 (${batchInfoKey}): ${batchInfoExists ? '存在' : '不存在'}`);
    
    if (batchInfoExists) {
      const batchInfo = await redis.get(batchInfoKey);
      console.log('📋 批次信息内容:');
      try {
        const parsedInfo = JSON.parse(batchInfo);
        console.log(JSON.stringify(parsedInfo, null, 2));
      } catch (e) {
        console.log('信息不是有效的JSON格式:', batchInfo.substring(0, 200) + '...');
      }
    }
    
    // 查找所有相关的批次键
    console.log('\n🔍 查找所有相关批次键...');
    const allKeys = await redis.keys('*batch*1763968563435*');
    if (allKeys.length > 0) {
      console.log('找到相关键:');
      allKeys.forEach(key => console.log(`  ${key}`));
      
      // 显示这些键的内容
      for (const key of allKeys) {
        const type = await redis.type(key);
        console.log(`\n🔑 键: ${key} (${type})`);
        
        if (type === 'string') {
          const value = await redis.get(key);
          try {
            const parsed = JSON.parse(value);
            console.log('值 (JSON):');
            console.log(JSON.stringify(parsed, null, 2));
          } catch (e) {
            console.log('值 (字符串):', value.substring(0, 200) + (value.length > 200 ? '...' : ''));
          }
        } else if (type === 'hash') {
          const hashData = await redis.hgetall(key);
          console.log('哈希数据:');
          Object.entries(hashData).forEach(([field, value]) => {
            console.log(`  ${field}: ${value}`);
          });
        }
      }
    } else {
      console.log('未找到相关批次键');
    }
    
  } catch (error) {
    console.error('❌ Redis操作出错:', error.message);
  } finally {
    await redis.quit();
  }
}

checkSpecificBatch();